/* 
 * Copyright (C) 2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;

namespace DCSharp
{
	public class IdentityEventArgs : EventArgs
	{
		private Identity identity;

		public IdentityEventArgs(Identity identity)
		{
			this.identity = identity;
		}

		public Identity Identity
		{
			get { return identity; }
		}
	}

	/// <summary>
	/// The identity of a user on a hub.
	/// </summary>
	public class Identity : ICloneable
	{
		private Dictionary<string, string> parameters;

		private Uid cid;
		private User user;
		private string nick;
		private string description;
		private string email;
		private string connection;
		private string client;
		private bool active;
		private bool isOperator;
		private long shareSize;

		public Identity(User user) : this(user.Uid, user.Nick)
		{
		}

		public Identity(User user, string nick) : this(user.Uid, nick)
		{
		}

		public Identity(Uid cid, string nick) : this()
		{
			if (nick == null)
			{
				throw new ArgumentNullException("nick");
			}
			this.cid = cid;
			this.nick = nick;
		}

		private Identity()
		{
			parameters = new Dictionary<string, string>();
		}

		#region Properties

		/// <summary>
		/// Gets the Cid of the user.
		/// </summary>
		/// <value>The Cid of the user</value>
		public Uid Cid
		{
			get { return cid; }
			set { cid = value; }
		}

		/// <summary>
		/// Gets the user this identity is about.
		/// </summary>
		/// <value>The user this identity is about.</value>
		public User User
		{
			get
			{
				if (user == null || user.Uid != cid)
				{
					user = User.Create(cid, nick);
				}
				return user;
			}
		}

		/// <summary>
		/// Gets or sets the nickname of the user.
		/// </summary>
		/// <value>The nickname of the user.</value>
		public string Nick
		{
			get { return nick; }
			set { nick = value; }
		}

		/// <summary>
		/// Gets or sets the user description.
		/// </summary>
		/// <value>The user description.</value>
		public string Description
		{
			get { return description; }
			set { description = value; }
		}

		/// <summary>
		/// Gets or sets the e-mail address of the user.
		/// </summary>
		/// <value>The e-mail address of the user.</value>
		public string Email
		{
			get { return email; }
			set { email = value; }
		}

		/// <summary>
		/// Gets or sets the connection of the user.
		/// </summary>
		/// <value>The connection of the user</value>
		public string Connection
		{
			get { return connection; }
			set { connection = value; }
		}

		/// <summary>
		/// Gets or sets the client of the user.
		/// </summary>
		/// <value>The client of the user</value>
		public string Client
		{
			get { return client; }
			set { client = value; }
		}

		/// <summary>
		/// Gets or sets whether or not the user is active.
		/// </summary>
		/// <value>Whether or not the user is active.</value>
		public bool Active
		{
			get { return active; }
			set { active = value; }
		}

		/// <summary>
		/// Gets or sets whether or not the user is an operator.
		/// </summary>
		/// <value>Whether or not the user is an operator.</value>
		public bool Op
		{
			get { return isOperator; }
			set { isOperator = value; }
		}

		/// <summary>
		/// Gets or sets the number of bytes shared by the user.
		/// </summary>
		/// <value>The number of bytes shared by the user.</value>
		public long ShareSize
		{
			get { return shareSize; }
			set { shareSize = value; }
		}

		#endregion

		#region Methods

		/// <summary>
		/// Retrives a user parameter by the parameter name.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <returns>The value of the parameter, if found; otherwise, null.</returns>
		internal string GetParameter(string name)
		{
			lock (parameters)
			{
				string value;
				if (parameters.TryGetValue(name, out value))
				{
					return value;
				}
				return null;
			}
		}

		/// <summary>
		/// Sets a user parameter by the parameter name.
		/// </summary>
		/// <param name="name">The name of the parameter.</param>
		/// <param name="value">The value of the parameter.</param>
		internal void SetParameter(string name, string value)
		{
			lock (parameters)
			{
				parameters[name] = value;
			}
		}

		/// <summary>
		/// Removes a user parameter.
		/// </summary>
		/// <param name="name">The parameter to remove.</param>
		internal void RemoveParameter(string name)
		{
			lock (parameters)
			{
				parameters.Remove(name);
			}
		}

		public object Clone()
		{
			return this.MemberwiseClone();
		}

		public override string ToString()
		{
			return String.Format("{0} ({1})", nick, cid);
		}

		#endregion
	}
}
